package com.hand.demo.app.service.impl;

import io.choerodon.core.domain.Page;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import com.hand.demo.app.service.HhrTaTrainingService;
import org.springframework.stereotype.Service;
import com.hand.demo.domain.entity.HhrTaTraining;
import com.hand.demo.domain.repository.HhrTaTrainingRepository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 培训经历表(HhrTaTraining)应用服务
 *
 * @author
 * @since 2025-06-16 12:16:50
 */
@Service
public class HhrTaTrainingServiceImpl implements HhrTaTrainingService {
    @Autowired
    private HhrTaTrainingRepository hhrTaTrainingRepository;

    @Override
    public Page<HhrTaTraining> selectList(PageRequest pageRequest, HhrTaTraining hhrTaTraining) {
        return PageHelper.doPageAndSort(pageRequest, () -> hhrTaTrainingRepository.selectList(hhrTaTraining));
    }

    @Override
    public void saveData(List<HhrTaTraining> hhrTaTrainings) {
        List<HhrTaTraining> insertList = hhrTaTrainings.stream().filter(line -> line.getId() == null).collect(Collectors.toList());
        List<HhrTaTraining> updateList = hhrTaTrainings.stream().filter(line -> line.getId() != null).collect(Collectors.toList());
        hhrTaTrainingRepository.batchInsertSelective(insertList);
        hhrTaTrainingRepository.batchUpdateByPrimaryKeySelective(updateList);
    }

    @Override
    public void deleteByResumeId(Long resumeId) {
        HhrTaTraining query = new HhrTaTraining();
        query.setResumeId(resumeId);
        List<HhrTaTraining> existingList = hhrTaTrainingRepository.selectList(query);
        if (!existingList.isEmpty()) {
            hhrTaTrainingRepository.batchDeleteByPrimaryKey(existingList);
        }
    }
}

